package com.jmyd.sharding.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.jmyd.commons.base.dto.KeywordQueryDto;
import com.jmyd.commons.base.service.impl.BaseServiceImpl;
import com.jmyd.commons.base.vo.PageBean;
import com.jmyd.coupon.server.service.IUserService;
import com.jmyd.sharding.server.domain.dto.UserDto;
import com.jmyd.sharding.server.domain.po.TUser;
import com.jmyd.sharding.server.domain.vo.UserVo;
import com.jmyd.sharding.server.mapper.TUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户业务层
 *
 * @author samphin
 * @date 2019-8-28 16:35:13
 */
@Service
public class UserService extends BaseServiceImpl<Long, UserDto, UserVo> implements IUserService {

    @Resource
    private TUserMapper userMapper;

    @Override
    public boolean save(UserDto dto) {
        TUser user = new TUser().build(dto);
        return this.userMapper.insertSelective(user) > 0;
    }

    @Override
    public boolean updateById(UserDto dto) {
        TUser user = new TUser().build(dto);
        return this.userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return this.userMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public PageBean<UserVo> queryPageList(KeywordQueryDto queryDto) {
        Page<Object> page = PageMethod.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        Example example = new Example(TUser.class);
        if (StringUtils.isNotBlank(queryDto.getKeyword())) {
            example.createCriteria().andLike("name", "%" + queryDto.getKeyword() + "%");
        }
        List<TUser> poList = this.userMapper.selectByExample(example);
        List<UserVo> voList = new UserVo().build(poList);
        return new PageBean<>(voList, page);
    }

    @Override
    public UserVo queryById(Long id) {
        TUser po = this.userMapper.selectByPrimaryKey(id);
        return new UserVo().build(po);
    }
}
